package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.entity.Collect;
import com.example.blog.mapper.CollectMapper;
import com.example.blog.service.ICollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
* @author Ading
* @description 针对表【collect】的数据库操作Service实现
* @createDate 2023-07-09 09:59:12
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
    implements ICollectService {

    @Resource
    CollectMapper collectMapper;


    /**
     * 根据文章ID和用户ID查询收藏记录
     *
     * @param articleId 文章ID
     * @param id        用户ID
     * @return 符合条件的收藏记录，若不存在则返回null
     */
    public Collect selectCobyId(int articleId, int id) {
        return collectMapper.selectOne( new LambdaQueryWrapper<Collect>()
                .eq(Collect::getArticleId, articleId)
                .eq(Collect::getUserId, id));
    }


    /**
     * 插入收藏记录
     *
     * @param articleId 文章ID
     * @param id        用户ID
     */
    public void insert(int articleId, int id){
        Collect collect = new Collect();
        collect.setUserId(id);
        collect.setArticleId(articleId);
        collect.setCreateTime(LocalDateTime.now());
        collectMapper.insert(collect);
    }



}




